import logging

import pytest

from resource.pom.settings import Settings
from utils.manager import Images
from resource.pom.system_ui import SystemUI


class Test:
    app = Settings
    system_ui = SystemUI

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.点击设置-WLAN，点击关闭WLAN按钮')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.wlan_item)
        device.refresh_layout()
        switch = device.get_element_by_type('Toggle')
        # wifi_status = device.get_wifi_status()
        if switch.get('checked') == 'true':
            device.click_element(switch)
        else:
            device.click_element(switch)
            device.wait(5)
            device.click_element(switch)
        logging.info('预期结果1：WLAN按钮不亮，不展示任何WLAN列表，且断开已连接的wifi，通知栏的wifi连接的图标不再显示')
        device.wait(5)
        device.refresh_layout()
        wifi_status = device.get_wifi_status()
        assert wifi_status.get('active') == 'inactive', 'wifi开关未关闭'
        assert wifi_status.get('connected') == 'not connected', 'wifi未断开连接'
        device.assert_text_exist('可用WLAN', expect=False)
        status_bar = device.save_snapshot_to_local('statusbar.jpeg')
        Images.crop_picture(status_bar, *self.system_ui.statusbar_area)
        assert not Images.is_image_in_image(self.system_ui.wlan_icon, status_bar), '状态栏有wifi图标'
        switch = device.get_element_by_type('Toggle')
        element_area = device.get_element_area(switch)
        wlan_off = device.save_snapshot_to_local('wlan_off.jpeg')
        wlan_off = Images.crop_picture(wlan_off, *element_area)
        similarity = Images.compare_image_similarity(wlan_off, self.app.switch_off)
        assert similarity > 0.9, '界面上wlan开关未显示为关闭状态'
